package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.ztgf.shopping.models.service.shop.IShopContractParamRelationService;
import com.ztgf.shopping.models.bo.shop.ShopContractParamRelationSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopContractParamRelationBO;
import com.ztgf.shopping.models.dto.shop.ShopContractParamRelationDTO;

/**
 * 店铺合同内容关联表控制器
 *
 * @author YUE
 * @Date 2020-07-21 11:56:39
 */

@RestController
@RequestMapping("/cms/shopContractParamRelation")
@Slf4j
@Api(value = "ShopContractParamRelationController",tags = "店铺合同内容关联表接口")
public class ShopContractParamRelationController {

    @DubboReference
    private IShopContractParamRelationService shopContractParamRelationService;

    @ApiOperation(value = "添加店铺合同内容关联表",notes = "添加店铺合同内容关联表")
    @PostMapping(value = "/insert")
    public Result insert(ShopContractParamRelationSaveBO saveBO) {
        boolean flag = shopContractParamRelationService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存店铺合同内容关联表",notes = "保存店铺合同内容关联表")
    public Result save(ShopContractParamRelationSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改店铺合同内容关联表",notes = "修改店铺合同内容关联表")
    @PostMapping("/update")
    public Result update(ShopContractParamRelationSaveBO saveBO){
        ShopContractParamRelationDTO dto = new ShopContractParamRelationDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = shopContractParamRelationService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除店铺合同内容关联表",notes = "删除店铺合同内容关联表")
    @ApiImplicitParam(name = "id",value = "店铺合同内容关联表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = shopContractParamRelationService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除店铺合同内容关联表",notes = "批量删除店铺合同内容关联表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        shopContractParamRelationService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopContractParamRelationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopContractParamRelationDTO dto = shopContractParamRelationService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "店铺合同内容关联表列表",notes = "店铺合同内容关联表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopContractParamRelationDTO>> queryPageList(ShopContractParamRelationBO bo){
        Wrapper<ShopContractParamRelationDTO> wrapper = new EntityWrapper<>();


        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ShopContractParamRelationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopContractParamRelationService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
